# -*- coding: utf-8 -*-
from typing import Generator
from numpy.core.numeric import flatnonzero
import scipy.io as scio
import numpy as np
import pandas as pd 
import os
import fnmatch
import pymongo


def readFile(path:str, col:pymongo.collection.Collection):
    # our sample step.
    period = 0.02
    # this data can got from the header.
    # original sample rate
    with open(path, mode="r") as f:
        while True:
            line = f.readline()
            if "SampleRate" in line:
                break
    line = line.split(",")
    source_freq = float(line[1].strip())
    priod_points = int(source_freq * period)
    # get the labels
    path_splited = path.split("/")
    # start with . means is the hidden data
    if path_splited[-1][0] == ".":
        # print(path)
        return
    labels = []
    if path_splited[-1][0] == "0":
        labels.append("0")
    else:
        labels.append(path_splited[-1][0])
        # isdigit is a function for str.m
        if path_splited[-1][1].isdigit():
            labels.append(path_splited[-1][1])
    sour_data = np.loadtxt(path, skiprows=16, delimiter=",", usecols=range(1,4))
    # only using two periods data
    normal_slice = sour_data[:2*priod_points,:]
    fault_slice = sour_data[-2*priod_points:,:]
    fault_dict = {"label":labels}
    normal_dict = {"label":["0"]}
    def save_to_dict(myDict, slice, sample_points=400)->dict:
        names = ["ia", "ib", "ic"]
        for name, i in zip(names, range(3)):
            data = slice[:,i]
            idx = np.linspace(0,len(data),
                    sample_points, endpoint=False).astype(np.int)
            myDict[name] = data[idx].tolist()
        return(myDict)
    fault_dict = save_to_dict(fault_dict, fault_slice)
    # fault_dict["path"] = path
    normal_dict = save_to_dict(normal_dict, normal_slice)
    # normal_dict["path"] = path
    col.insert_many([normal_dict, fault_dict])



# def retrieve_eles(path, path_list):
#     ele_list = os.listdir(path)
#     for ele in ele_list:
#         ele_path = os.path.join(path, ele)
#         if os.path.isfile(ele_path):
#             path_list.append(ele_path)
#         else:
#             return(ele_path, path_list)

def retrieve_files(path:str) -> Generator:
    path_gen = os.walk(path)
    for root, _, files in path_gen:
        for name in files:
           yield(os.path.join(root,name))
        

if __name__ == "__main__":
    scriptDir = os.path.dirname(os.path.realpath(__file__))
    basepath = os.path.dirname(os.path.dirname(scriptDir))
    dataDir = os.path.join(basepath, "data")
    dataDir = os.path.join(dataDir, "邓茜实验数据")
    client = pymongo.MongoClient("mongodb://127.0.0.1:27017/")
    db  = client["Power_Fault"]
    col_sour = db["data_sour_exper"]
    print(type(col_sour))
    file_gen = retrieve_files(dataDir)
    for file in file_gen:
        try:
            readFile(file, col_sour)
        except:
            print(file)
            raise
    
                
